package com._21cn._front.product.api;

import com._21cn._front.utils.wxin.Sign;
import com._21cn.framework.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/12.
 */
@Controller
@RequestMapping({"/api/car/wxin"})
public class WXinApiController {



    @RequestMapping("getSign")
    @ResponseBody
    public String getSign(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        String url = request.getParameter("url");
        String jsoncallback = request.getParameter("jsoncallback");
        String timeStr = Sign.createTimeStr();
        String nonceStr = Sign.createNonceStr();
        String jsapiTicket = Sign.getTicket();
        String signature = Sign.sign(timeStr, nonceStr, jsapiTicket, url);

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("timeStr", timeStr);
        resultMap.put("nonceStr", nonceStr);
        resultMap.put("signature", signature);
        resultMap.put("url", url);
        String json = JSONObject.toJSONString(resultMap);
        if( !StringUtil.isEmpty(jsoncallback) ){
            json = jsoncallback+"("+json+")";
        }
        return json;
    }

}
